package com.foreverwin.jzymes.production.gr.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.ICommonService;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.gr.domain.GoodsReceiptLog;
import com.foreverwin.jzymes.production.gr.service.IGoodsReceiptLogAdviceService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.mapper.SFCAdviceMapper;
import com.foreverwin.jzymes.production.sfc.mapper.SFCMapper;
import com.foreverwin.jzymes.report.gr.service.IGoodsReceiptReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/gr")
public class GoodsReceiptController {

    @Autowired
    IGoodsReceiptLogAdviceService goodsReceiptLogAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IGoodsReceiptReportService goodsReceiptReportService;
    @Autowired
    ICommonService commonService;
    @Autowired
    SFCAdviceMapper sfcAdviceMapper;
    @Autowired
    SFCMapper sfcMapper;

    @RequestMapping("/goodsReceive")
    @ParamBind
    public AjaxResult goodsReceive( ArrayList<GoodsReceiptLog> goodsReceiptLogs ){
        try{
            Map<String,String> rtnMap = goodsReceiptLogAdviceService.goodsReceive( goodsReceiptLogs );
            return AjaxResult.returnSuccess().put( "value", rtnMap );
        }catch (BusinessException e){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping("/item")
    public AjaxResult findItemByItemGroup(){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        /**机种*/
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemTreeList = new ArrayList<>();
        commonService.findItemTree( site, itemGroup, itemTreeList, true, null );
        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMap : itemTreeList ){
            String itemLabel = customFieldsAdviceService.existCustomValue( (String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
            String description = StringUtils.isBlank( itemLabel ) ? (String) itemMap.get( "DESCRIPTION" ) : itemLabel;

            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", itemMap.get( "ITEM_GROUP" ) );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", description );
            rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
            rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
            rtnItemMap.put( "storageLocation", itemMap.get( "ERP_PUTAWAY_STORLOC" ) );
            rtnItemMap.put( "itemType", itemMap.get( "ITEM_TYPE" ) );
            rtnItemList.add( rtnItemMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    @RequestMapping("/item/sfc")
    public AjaxResult findSfcByItem( @RequestParam String itemBo ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );

        itemBo = commonService.findSfcItemByItem( site, itemBo );

        List<SFC> sfcList = null;
        sfcList = sfcAdviceMapper.selectSFCListOnWorkByItem( site, itemBo );

        List<Map<String,Object>> rtnSfcList = new ArrayList<>();
        for( SFC sfc : sfcList ){
            Map<String,Object> sfcMap = new HashMap<>();
            Map<String,Object> itemMap = itemAdviceService.existItem( sfc.getItemBo() );

            sfcMap.put( "sfc", sfc.getSfc() );
            sfcMap.put( "machineType", sfc.getMachineType() );
            sfcMap.put( "itemBo", sfc.getItemBo() );
            sfcMap.put( "item", itemMap.get( "ITEM" ) );
            sfcMap.put( "revision", itemMap.get( "REVISION" ) );
            sfcMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnSfcList.add( sfcMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnSfcList );
    }

    /**
     * 验证物料工令
     *
     * @param itemBo
     * @param sfc
     * @return
     */
    @RequestMapping("/item/existSfc")
    public AjaxResult existSfcByItem( @RequestParam String itemBo, @RequestParam String sfc ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        String _itemBo = itemBo;

        itemBo = commonService.findSfcItemByItem( site, itemBo );

        SFC _sfc = sfcMapper.selectSFCById( BOHandleUtil.buildSFCHandle( site, sfc ) );
        Assert.notNull( _sfc, I18nUtil.getI18nText( "production.sfc.sfcNotExist", new String[]{ sfc } ) );
        if( _sfc.getActEndTime() != null ){
            throw BusinessException.build( "production.sfc.sfcDone", new String[]{ sfc } );
        }

        if( !_sfc.getItemBo().equals( itemBo ) ){
            throw BusinessException.build( "production.sfc.sfcNotMatchItem", new String[]{ sfc, _itemBo } );
        }

        Map<String,Object> sfcMap = new HashMap<>();
        Map<String,Object> itemMap = itemAdviceService.existItem( _sfc.getItemBo() );
        sfcMap.put( "sfc", _sfc.getSfc() );
        sfcMap.put( "machineType", _sfc.getMachineType() );
        sfcMap.put( "itemBo", _sfc.getItemBo() );
        sfcMap.put( "item", itemMap.get( "ITEM" ) );
        sfcMap.put( "revision", itemMap.get( "REVISION" ) );
        sfcMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );

        return AjaxResult.returnSuccess().put( "value", sfcMap );
    }

    @RequestMapping("/existItem")
    public AjaxResult existSfcItem( @RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( item, I18nUtil.getI18nText( "common.itemNotBlank" ) );

        Map<String,Object> itemMap = itemAdviceService.existItem( site, item );
        String itemBo = (String) itemMap.get( "HANDLE" );

        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        String _itemGroup = commonService.findItemGroupByItem( site, itemBo );
        if( _itemGroup == null ){
            throw BusinessException.build( "common.itemNotMatchValidItemGroup", new String[]{ item } );
        }else{
            String customValue = customFieldsAdviceService.findCustomValue( BOHandleUtil.buildItemGroupHandle( site, _itemGroup ), Constants.ITEM_GROUP_CUSTOM_FIELD_REFER_ITEM_GROUP );
            if( !itemGroup.equals( _itemGroup ) && !itemGroup.equals( customValue ) ){
                throw BusinessException.build( "common.itemNotMatchValidItemGroup", new String[]{ item } );
            }
        }

        String itemLabel = customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
        String description = StringUtils.isBlank( itemLabel ) ? (String) itemMap.get( "DESCRIPTION" ) : itemLabel;
        Map<String,Object> rtnItemMap = new HashMap<>();
        rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
        rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
        rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
        rtnItemMap.put( "description", description );
        rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
        rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
        rtnItemMap.put( "storageLocation", itemMap.get( "ERP_PUTAWAY_STORLOC" ) );
        rtnItemMap.put( "itemType", itemMap.get( "ITEM_TYPE" ) );
        return AjaxResult.returnSuccess().put( "value", rtnItemMap );
    }



}
